﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess.Loan;
using System.Data;
using FOModels.Loan;
namespace Bussiness.Loan
{
    public class LoanBLL
    {
        private LoanDAL dal;
        public LoanBLL()
        {
            dal = new LoanDAL();
        }

        public List<LoanPlanCategoryModels> getLoanPlan()
        {
            List<LoanPlanCategoryModels> list = new List<LoanPlanCategoryModels>();
            LoanPlanCategoryModels fur;
            DataTable tb = dal.getloanplancatelist();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanPlanCategoryModels();
                    fur.LoanPlanCategoryID = int.Parse(row[0].ToString());
                    fur.LoanPlanCategoryName = row[1].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<LoanCategoryModels> getLoanCate()
        {
            List<LoanCategoryModels> list = new List<LoanCategoryModels>();
            LoanCategoryModels fur;
            DataTable tb = dal.getloancatelist();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanCategoryModels();
                    fur.LoanCategoryID = int.Parse(row[0].ToString());
                    fur.LoanCategoryName = row[1].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<LoanModels> getdefaultloan(int cateid, int planid)
        {
            List<LoanModels> list = new List<LoanModels>();
            LoanModels fur;
            DataTable tb = dal.getdefaultloan(cateid, planid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanModels();
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<LoanModels> getloanjson()
        {
            List<LoanModels> list = new List<LoanModels>();
            LoanModels fur;
            DataTable tb = dal.getloanjson();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanModels();
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                    fur.LoanCategoryName = row[17].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public LoanModels getloanbyid(int loanid)
        {
         
            LoanModels fur = new LoanModels();
            DataTable tb = dal.getloanbyid(loanid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                }
                return fur;
            }
            else
            {
                return null;
            }
      
        }
        public List<LoanModels> getloan(int cateid, int planid, string balance)
        {
            List<LoanModels> list = new List<LoanModels>();
            LoanModels fur;
            DataTable tb = dal.getLoan(cateid, planid,balance);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanModels();
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }

        public int UpdateLoan(LoanModels models)
        {
            if (dal.UpdateLoan(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertLoan(LoanModels models)
        {
            if (dal.InsertLoan(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
       
        public int DeleteLoan(int LoanID)
        {
            if (dal.DeleteLoan(LoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }


        ///EN
        ///
        public List<LoanPlanCategoryModels> getLoanPlan_EN()
        {
            List<LoanPlanCategoryModels> list = new List<LoanPlanCategoryModels>();
            LoanPlanCategoryModels fur;
            DataTable tb = dal.getloanplancatelist_EN();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanPlanCategoryModels();
                    fur.LoanPlanCategoryID = int.Parse(row[0].ToString());
                    fur.LoanPlanCategoryName = row[1].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<LoanCategoryModels> getLoanCate_EN()
        {
            List<LoanCategoryModels> list = new List<LoanCategoryModels>();
            LoanCategoryModels fur;
            DataTable tb = dal.getloancatelist_EN();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanCategoryModels();
                    fur.LoanCategoryID = int.Parse(row[0].ToString());
                    fur.LoanCategoryName = row[1].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<LoanModels> getdefaultloan_EN(int cateid, int planid)
        {
            List<LoanModels> list = new List<LoanModels>();
            LoanModels fur;
            DataTable tb = dal.getdefaultloan_EN(cateid, planid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanModels();
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<LoanModels> getloanjson_EN()
        {
            List<LoanModels> list = new List<LoanModels>();
            LoanModels fur;
            DataTable tb = dal.getloanjson_EN();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanModels();
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                    fur.LoanCategoryName = row[17].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public LoanModels getloanbyid_EN(int loanid)
        {

            LoanModels fur = new LoanModels();
            DataTable tb = dal.getloanbyid_EN(loanid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                }
                return fur;
            }
            else
            {
                return null;
            }

        }
        public List<LoanModels> getloan_EN(int cateid, int planid, string balance)
        {
            List<LoanModels> list = new List<LoanModels>();
            LoanModels fur;
            DataTable tb = dal.getLoan_EN(cateid, planid, balance);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new LoanModels();
                    fur.LoanID = int.Parse(row[0].ToString());
                    fur.LoanName = row[1].ToString();
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[2].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.Benefit = row[4].ToString();
                    fur.InterestRate = float.Parse(row[5].ToString());
                    fur.CompanyID = int.Parse(row[6].ToString());
                    fur.LoanCategoryID = int.Parse(row[7].ToString());
                    fur.LoanPlanCategoryID = int.Parse(row[8].ToString());
                    fur.CompanyName = row[11].ToString();
                    fur.CompanyImg = row[14].ToString();
                    fur.Description = row[9].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }

        public int UpdateLoan_EN(LoanModels models)
        {
            if (dal.UpdateLoan_EN(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertLoan_EN(LoanModels models)
        {
            if (dal.InsertLoan_EN(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        public int DeleteLoan_EN(int LoanID)
        {
            if (dal.DeleteLoan_EN(LoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
